﻿// Copyright © Microsoft Corporation.  All Rights Reserved.
// This code released under the terms of the 
// Microsoft Public License (MS-PL, http://opensource.org/licenses/ms-pl.html.)

using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using TheOliver.DigitalSignage.Admin.Controls;
using Web;
using Web.Services;

namespace TheOliver.DigitalSignage.Admin.Views
{
    public partial class Slots : Page
    {
        List<Web.Room> _rooms;
        List<Web.Session> _sessions;
        List<Web.Speaker> _speakers;

        bool _roomsLoaded = false;
        bool _sessionsLoaded = false;
        bool _speakersLoaded = false;

        public Slots()
        {
            InitializeComponent();
        }

        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }

        private void eventDomainDataSource_LoadedData(object sender, LoadedDataEventArgs e)
        {
            if (e.HasError)
            {
                System.Windows.MessageBox.Show(e.Error.ToString(), "Load Error", System.Windows.MessageBoxButton.OK);
                e.MarkErrorAsHandled();
            }
            else
            {
                sessionDomainDataSource.Load();
                speakerDomainDataSource.Load();
            }
            _eventCombo.SelectedIndex = -1;
        }

        private void LoadRooms()
        {
            Web.Event ev = _eventCombo.SelectedItem as Web.Event;

            if (ev != null)
            {
                roomDomainDataSource.QueryParameters.Clear();
                System.Windows.Data.Parameter param = new System.Windows.Data.Parameter();
                param.ParameterName = "eventId";
                param.Value = ev.Id;
                roomDomainDataSource.QueryParameters.Add(param);
                roomDomainDataSource.Load();
            }
        }

        private void roomDomainDataSource_LoadedData(object sender, LoadedDataEventArgs e)
        {
            _roomsLoaded = true;
            if (e.HasError)
            {
                System.Windows.MessageBox.Show(e.Error.ToString(), "Load Error", System.Windows.MessageBoxButton.OK);
                e.MarkErrorAsHandled();
            }
            else
            {
                //_rooms = new List<Room>();
                //DigitalSignageDomainContext context = roomDomainDataSource.DomainContext as DigitalSignageDomainContext;
                //foreach (Web.Room room in context.Rooms)
                //{
                //    _rooms.Add(room);
                //}
            }
            //LoadSlots();
        }

        private void LoadSlots()
        {
            Web.Room room = _roomCombo.SelectedItem as Web.Room;

            if (room != null)
            {
                slotDomainDataSource.QueryParameters.Clear();
                System.Windows.Data.Parameter param = new System.Windows.Data.Parameter();
                param.ParameterName = "roomId";
                param.Value = room.Id;
                slotDomainDataSource.QueryParameters.Add(param);
                slotDomainDataSource.Load();
            }
        }

        private void slotDomainDataSource_LoadedData(object sender, LoadedDataEventArgs e)
        {
            if (e.HasError)
            {
                System.Windows.MessageBox.Show(e.Error.ToString(), "Load Error", System.Windows.MessageBoxButton.OK);
                e.MarkErrorAsHandled();
            }
        }

        private void sessionDomainDataSource_LoadedData(object sender, LoadedDataEventArgs e)
        {
            _sessionsLoaded = true;
            if (e.HasError)
            {
                System.Windows.MessageBox.Show(e.Error.ToString(), "Load Error", System.Windows.MessageBoxButton.OK);
                e.MarkErrorAsHandled();
            }
            else
            {
                _sessions = new List<Web.Session>();
                DigitalSignageDomainContext context = 
                    sessionDomainDataSource.DomainContext as DigitalSignageDomainContext;
                foreach (Web.Session session in context.Sessions)
                {
                    _sessions.Add(session);
                }
            }
            LoadSlots();
        }

        private void speakerDomainDataSource_LoadedData(object sender, LoadedDataEventArgs e)
        {
            _speakersLoaded = true;
            if (e.HasError)
            {
                System.Windows.MessageBox.Show(e.Error.ToString(), "Load Error", System.Windows.MessageBoxButton.OK);
                e.MarkErrorAsHandled();
            }
            else
            {
                _speakers = new List<Web.Speaker>();
                DigitalSignageDomainContext context =
                    speakerDomainDataSource.DomainContext as DigitalSignageDomainContext;
                foreach (Web.Speaker speaker in context.Speakers)
                {
                    _speakers.Add(speaker);
                }
            }
            LoadSlots();
        }

        private void _save_Click(object sender, RoutedEventArgs e)
        {
            slotDomainDataSource.SubmitChanges();
        }

        private void _reject_Click(object sender, RoutedEventArgs e)
        {
            slotDomainDataSource.RejectChanges();
        }

        private void _add_Click(object sender, RoutedEventArgs e)
        {
            AddSlot();
        }

        private void _remove_Click(object sender, RoutedEventArgs e)
        {
            if (slotDataGrid.SelectedItem != null)
            {
                QuestionWindow qw = 
                    new QuestionWindow(TextResource.DeleteSlotQuestion, DeleteSlot);
                qw.Show();
            }
        }

        private void AddSlot()
        {
            Web.Slot slot = new Web.Slot();
            slot.Date = DateTime.Today;
            slot.Time = new TimeSpan(9, 0, 0);

            DigitalSignageDomainContext context =
                slotDomainDataSource.DomainContext as DigitalSignageDomainContext;

            context.Slots.Add(slot);

            slotDataGrid.DataContext = context;
        }

        private void DeleteSlot()
        {
            Web.Slot slot = slotDataGrid.SelectedItem as Web.Slot;
            DigitalSignageDomainContext context =
                slotDomainDataSource.DomainContext as DigitalSignageDomainContext;

            context.Slots.Remove(slot);
            slotDataGrid.DataContext = context;
        }

        private void roomId_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ComboBox cb = sender as ComboBox;
            Web.Slot slot = cb.DataContext as Web.Slot;

            Room room = cb.SelectedItem as Web.Room;
            slot.RoomId = room.Id;
        }

        private void roomId_Loaded(object sender, RoutedEventArgs e)
        {
            ComboBox cb = sender as ComboBox;
            
            cb.ItemsSource = _rooms;
            cb.SelectedIndex = -1;
            Web.Slot slot = cb.DataContext as Web.Slot;
            if (slot != null)
            {
                if (slot.RoomId.HasValue)
                {
                    bool found = false;
                    foreach (Web.Room room in cb.Items)
                    {
                        if (room.Id == slot.RoomId)
                        {
                            cb.SelectedItem = room;
                            return;
                        }
                    }
                    if (!found)
                    {
                        cb.SelectedItem = null;
                    }
                }
                else
                {
                    cb.SelectedItem = null;
                }
            }
        }

        #region Session Combo

        private void sessionId_Loaded(object sender, RoutedEventArgs e)
        {
            ComboBox cb = sender as ComboBox;
            cb.ItemsSource = _sessions;

            Web.Slot slot = cb.DataContext as Web.Slot;
            foreach (Web.Session session in cb.Items)
            {
                if (session != null)
                {
                    if (session.Id == slot.SessionId)
                    {
                        cb.SelectedItem = session;
                        return;
                    }
                }
                else
                {
                    cb.SelectedItem = null;
                }
            }
        }

        private void sessionId_Changed(object sender, SelectionChangedEventArgs e)
        {
            ComboBox cb = sender as ComboBox;
            Web.Slot slot = cb.DataContext as Web.Slot;

            Web.Session session = cb.SelectedItem as Web.Session;
            slot.SessionId = session.Id;
        }

        #endregion

        #region Speaker Combo

        private void speakerId_LayoutUpdated(object sender, EventArgs e)
        {
            
        }

        private void speakerId_Loaded(object sender, RoutedEventArgs e)
        {
            ComboBox cb = sender as ComboBox;
            cb.ItemsSource = _speakers;

            Web.Slot slot = cb.DataContext as Web.Slot;

            if (slot.SpeakerId.HasValue)
            {
                for (int i = 0; i < cb.Items.Count; i++)
                {
                    Web.Speaker s = cb.Items[i] as Web.Speaker;
                    if (s.Id == slot.SpeakerId)
                    {
                        cb.SelectedIndex = i;
                        return;
                    }
                }
            }
            //bool found = false;
            //foreach (Web.Speaker speaker in cb.Items)
            //{
            //    if (speaker.Id == slot.SpeakerId)
            //    {
            //        cb.SelectedItem = speaker;
            //        return;
            //    }
            //}
        }

        private void speakerId_Changed(object sender, SelectionChangedEventArgs e)
        {
            ComboBox cb = sender as ComboBox;
            Web.Slot slot = cb.DataContext as Web.Slot;

            Web.Speaker speaker = cb.SelectedItem as Web.Speaker;
            slot.SpeakerId = speaker.Id;
        }

        #endregion

        private void _eventCombo_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            LoadRooms();
        }

        private void _roomCombo_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            LoadSlots();
        }
    }
}
